<template>
	<view class="">
		<view class="" style="padding-bottom: 120rpx;">
			<view class="mainpadding ffffff">
				<!-- <view class="ershiba nofonweight xiaohei">场地主图</view>
				<view class="margin_top shangc" v-if="!image" @click="uploadImg(1)">
					<image src="../static/image/system/shangchuan.png" mode=""></image>
				</view>
				<view class="margin_top shangc" v-if="image" @click="uploadImg(1)">
					<image :src="image_text" mode=""></image>
				</view>
				<view class="ershiba nofonweight xiaohei margin_top">场地轮播图（{{images_text.length}}/5）</view>
				<view class="flexleft flex_wrap">
					<view class="shilitu margin_top margin_right2 dingwei" v-for="(item,index) in images_text"
						:key="index">
						<image :src="item" mode=""></image>
						<view class="shanchu" @click="delindex(index,images_text,images)">
							<u-icon name="close-circle-fill"></u-icon>
						</view>
					</view>
					<view class="margin_top shangc margin_right2" v-if="images.length<5"
						@click="uploadImg(5,images,images_text)">
						<image src="../static/image/system/shangchuan.png" mode=""></image>
					</view>
				</view> -->
				<view class="ershiba nofonweight xiaohei margin_top">场地名称</view>
				<view class="margin_top">
					<u--input placeholder="请输入场地名称" border="surround" v-model="name"></u--input>
				</view>
				<view class="ershiba nofonweight xiaohei margin_top">权重</view>
				<view class="margin_top">
					<u--input placeholder="权重越大,位置越前" type="number" border="surround" v-model="weigh"></u--input>
				</view>
				<!-- <view class="ershiba nofonweight xiaohei margin_top">场地详情图（{{detail_images_text.length}}/5）</view>
				<view class="flexleft flex_wrap">
					<view class="shilitu margin_top margin_right2 dingwei" v-for="(item,index) in detail_images_text"
						:key="index">
						<image :src="item" mode=""></image>
						<view class="shanchu" @click="delindex(index,detail_images_text,detail_images)">
							<u-icon name="close-circle-fill"></u-icon>
						</view>
					</view>
					<view class="margin_top shangc margin_right2" v-if="detail_images.length<5"
						@click="uploadImg(5,detail_images,detail_images_text)">
						<image src="../static/image/system/shangchuan.png" mode=""></image>
					</view>
				</view> -->
			</view>
			<!-- <view class="mainpadding ffffff margin_top1">
				<uni-data-picker @change="bindPickerChange" :localdata="items" placeholder="请选择住址" popup-title="请选择地区"
					v-model="area_id" class="zdy">
					<view class="xiahuaxian margin_top flexbetween">
						<view class="ershiba xiaohei nofonweight">城市</view>
						<view class="flexright">
							<input type="text" class="textright" placeholder="请选择" v-model="city_text" disabled=""
								style="width: 80%;">
							<u-icon name="arrow-right"></u-icon>
						</view>
					</view>
				</uni-data-picker>
				<view class="xiahuaxian flexbetween margin_top" @click="chooselocation">
					<view class="ershiba nofonweight xiaohei">地址</view>
					<view class="flexright">
						<input type="text" class="textright" placeholder="请选择" v-model="address" disabled=""
							style="width: 80%;">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top" @click="messageTime.timeShow = true;timevalue=1">
					<view class="ershiba nofonweight xiaohei">开始营业时间</view>
					<view class="flexright">
						<input type="text" class="textright" placeholder="请选择" v-model="start_time" disabled=""
							style="width: 80%;">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top" @click="messageTime.timeShow = true;timevalue=2">
					<view class="ershiba nofonweight xiaohei">结束营业时间</view>
					<view class="flexright">
						<input type="text" class="textright" placeholder="请选择" v-model="end_time" disabled=""
							style="width: 80%;">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">联系电话</view>
					<input type="text" class="textright" placeholder="请输入" v-model="mobile" style="width: 80%;">
				</view>
				<view class="xiahuaxian flexbetween margin_top" @click="xmShow = true">
					<view class="ershiba nofonweight xiaohei">项目</view>
					<view class="flexright">
						<input class="textright" type="text" placeholder="请选择" disabled="" v-model="cg_category_name">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="xiahuaxian">
					<view class=" flexbetween margin_top">
						<view class="ershiba nofonweight xiaohei">标签</view>
						<view class="flexright">
							<input class="textright margin_right2" type="text" placeholder="请输入后添加" v-model="bqtext"
								@confirm="addsheshi">
							<u-icon name="plus-square-fill" color="#1BA95B" size="28" @click="addsheshi"></u-icon>
						</view>
					</view>
					<view class="flexleft flex_wrap">
						<view class="lvbtn margin_top1 margin_right1 flexleft" v-for="(item,index) in bqarr"
							:key="index">
							<text class="margin_right1">{{item}}</text>
							<u-icon name="close-circle" color="#999" size="16" @click="bqarr.splice(index,1)"></u-icon>
						</view>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top" @click="bqshow = true">
					<view class="ershiba nofonweight xiaohei">场地标签</view>
					<view class="flexright">
						<input class="textright" type="text" placeholder="请选择" v-model="selectbqtext" disabled="">
						<u-icon name="arrow-right"></u-icon>

					</view>
				</view>

				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">推荐状态</view>
					<view class="flexright">
						<u-radio-group v-model="is_rec" placement="row" circle>
							<u-radio activeColor="#1ba95b" :customStyle="{marginRight: '30rpx'}" label="不推荐"
								name="1"></u-radio>
							<u-radio activeColor="#1ba95b" label="推荐" name="2"></u-radio>
						</u-radio-group>
					</view>
				</view>
			</view> -->
		</view>
		<view class="gudingdb ffffff mainpadding">
			<view class="bigbtn" @click="submit">保存</view>
		</view>
		<timePicker :messageTime="messageTime" @timeConfirm="timeConfirm"></timePicker>
		<u-picker :show="xmShow" :columns="xmList" title="选择类型" @cancel="xmShow=false" @confirm="xmconfirm"
			keyName="name"></u-picker>
		<u-popup :show="bqshow" @close="bqshow=false">
			<view class="mainpadding ">
				<view class="mainpadding textcenter titletext fonweight">场地标签</view>
				<view class="flexleft flex_wrap margin_top">
					<!-- 未选中 -->
					<view :class="item.ischeck?'lvbtn margin_right1 margin_top1':'huibtn margin_right1 margin_top1'"
						v-for="item in cdbqList" :key="item.id" @click="checkssbq(item)">{{item.name}}</view>
				</view>
			</view>

		</u-popup>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import citys from "@/static/city/city.js"
	export default {
		data() {
			return {
				show: false,
				image: "",
				image_text: "",
				images: [],
				images_text: [],
				name: "",
				weigh:"",
				detail_images: [],
				detail_images_text: [],
				kc_des: "",
				type: "1", //类型:1=线下,2=线上
				address: "",
				province_id: "",
				city_id: "",
				area_id: "",
				latitude: "",
				longitude: "",
				start_time: "",
				end_time: "",
				mobile: "",
				is_rec: "2", //推荐状态
				cg_category_id: "", //项目
				cg_category_name: "",
				items: citys.data,
				city_text: "",
				upvalue: 0,
				xmShow: false,
				xmList: [],
				listid: "",
				// 场地标签
				cdbqList: [],
				bqtext: "",
				bqshow: false,
				bqarr: [],
				messageTime: {
					timeShow: false,
					title: "选择时间",
					mode: "time", //date日期，datetime年月日时分秒，time时间
				},
				timevalue: 0, //1开始,2结束
				cg_bq_ids: [],
				selectbqtext:"",
				user_zbfapply_listid:""
			}
		},
		onLoad(options) {
			this.user_zbfapply_listid=options.user_zbfapply_listid
			if (options.id) {
				uni.setNavigationBarTitle({
					title: "编辑场地"
				})
				this.listid = options.id
				this.getDetail()
				this.getcgfl() //场馆分类/项目
			} else {
				this.getcgfl() //场馆分类/项目
				this.getssbq() //场地标签
			}
		},
		methods: {
			addsheshi() {
				if (this.bqtext == "") {
					httpRequest.toast("请输入后添加")
					return false
				}
				this.bqarr.push(this.bqtext);
				this.bqtext = ''
			},
			ssbqListfn() {
				let arr = []
				this.cdbqList.forEach(item => {
					if (item.ischeck) {
						arr.push(item.name)
					}
				})
				this.selectbqtext = arr.length ? arr.join() : ""
			},
			chooselocation() {
				let _this = this
				uni.chooseLocation({
					success: function(response) {
						_this.address = response.address
						_this.latitude = response.latitude
						_this.longitude = response.longitude
					},
					fail(res) {
						console.log(res, "失败了");
					}
				})
			},
			timeConfirm(e) {
				if (this.timevalue == 1) this.start_time = e
				if (this.timevalue == 2) this.end_time = e
			},
			checkssbq(item) {
				item.ischeck = !item.ischeck
				this.ssbqListfn()
			},
			getssbq() {
				httpRequest.request('/api/cgcl/cgBqIndex', 'POST', {}, false, false, true).then(res => {
					res.data.forEach(item => {
						this.$set(item, "ischeck", false)
						this.cg_bq_ids.forEach(ite => {
							if (ite == item.id) {
								item.ischeck = true
							}
						})
					})
					this.cdbqList = res.data
					console.log(this.cdbqList,23823);
					this.ssbqListfn()
				})
			},
			getDetail() {
				httpRequest.request('/api/cgcl/myCgDetail', 'GET', {
					id: this.listid,
					user_zbfapply_id:this.user_zbfapply_listid
				}).then(res => {
					let data = res.data
					this.name = data.name
					this.weigh = data.weigh
					this.image = data.image
					this.image_text = data.image_text
					this.images = data.images.split(",")
					this.images_text = data.images_text
					this.detail_images = data.detail_images.split(",")
					this.detail_images_text = data.detail_images_text
					
					this.address = data.address
					this.longitude = data.longitude
					this.latitude = data.latitude
					this.is_rec = data.is_rec.toString()
					this.cg_category_id = data.cg_category_id
					this.cg_category_name = data.cgcategory.name
					this.province_id = data.province_id
					this.city_id = data.city_id
					this.area_id = data.area_id
					this.city_text = data.cityinfo
					this.start_time = data.start_time
					this.end_time = data.end_time
					this.mobile = data.mobile
					this.bqarr = data.bq_names_text
					this.cg_bq_ids = data.cg_bq_ids.split(',')
					this.getssbq() //场地标签
				})
			},
			submit() {
				// if (this.image == "") {
				// 	httpRequest.toast("请上传场地主图")
				// 	return false
				// }
				// if (!this.images.length) {
				// 	httpRequest.toast("请上传场地轮播图")
				// 	return false
				// }
				if (this.name == "") {
					httpRequest.toast("请输入场地名称")
					return false
				}
				// if (!this.detail_images.length) {
				// 	httpRequest.toast("请上传场地详情图")
				// 	return false
				// }
				// if (this.area_id == "") {
				// 	httpRequest.toast("请选择城市")
				// 	return false
				// }
				// if (this.start_time == "") {
				// 	httpRequest.toast("请选择开始营业时间")
				// 	return false
				// }
				// if (this.end_time == "") {
				// 	httpRequest.toast("请选择结束营业时间")
				// 	return false
				// }
				// if (this.mobile == "") {
				// 	httpRequest.toast("请输入联系电话")
				// 	return false
				// }
				// let arr = []
				// this.cdbqList.forEach(item => {
				// 	if (item.ischeck) {
				// 		arr.push(item.id)
				// 	}
				// })
				// if (!arr.length) {
				// 	httpRequest.toast("请至少选择一个场地标签")
				// 	return false
				// }
				// if (this.cg_category_id == "") {
				// 	httpRequest.toast("请选择项目")
				// 	return false
				// }
				let url = "/api/cgcl/addCg"
				let data = {
					// image: this.image,
					// images: this.images.join(),
					// detail_images: this.detail_images.join(),
					name: this.name,
					weigh: this.weigh,
					// province_id: this.province_id,
					// city_id: this.city_id,
					// area_id: this.area_id,
					// address: this.address,
					// longitude: this.longitude,
					// latitude: this.latitude,
					// cg_category_id: this.cg_category_id,
					// cg_bq_ids: arr.join(),
					// bq_names: this.bqarr.join(),
					// start_time: this.start_time,
					// end_time: this.end_time,
					// mobile: this.mobile,
					// is_rec: this.is_rec,
					user_zbfapply_id:this.user_zbfapply_listid
				}
				if (this.listid) {
					url = "/api/cgcl/editCg"
					this.$set(data, "id", this.listid)
				}
				httpRequest.request(url, 'POST', data).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			},
			xmconfirm(e) {
				this.cg_category_id = e.value[0].id
				this.cg_category_name = e.value[0].name
				this.xmShow = false
			},
			getcgfl() {
				httpRequest.request('/api/index/cgCategory', 'POST', {}, false, false, true).then(res => {
					this.xmList.push(res.data)
				})
			},
			// 跳转完成创建
			tzwccj() {
				uni.navigateTo({
					url: '/pages_julebu/chuangjianwc'
				})
			},
			bindPickerChange(val) {
				let area = val.detail.value
				let area_text = [area[0].text, area[1].text, area[2].text];
				this.city_text = area_text.join("/")
				this.province_id = area[0].value;
				this.city_id = area[1].value;
				this.area_id = area[2].value;
			},
			confirm(e) {
				console.log(e)
				this.show = false
			},
			cancel() {
				this.show = false
			},
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				this.upvalue = value
				let num = 0
				if (value == 5) {
					num = Number(value) - upImgs.length;
					if (num <= 0) {
						return false;
					}
				} else {
					num = 1
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			delindex(index, upImg, upImgs_text) {
				upImg.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					if (this.upvalue == 1) {
						this.image = res.url
						this.image_text = image_item
						return false
					}
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
			upvideo() { //上传视频
				let uploadFile = ''
				let _this = this;
				// 1.选择要上传的视频
				uni.chooseVideo({
					maxDuration: 60, // 拍摄视频最长拍摄时间，单位秒。最长支持 60 秒。
					sourceType: ['album', 'camera'], // album 从相册选视频，camera 使用相机拍摄，默认为：['album', 'camera']
					success: function(res) {
						_this.uploadImageServicet(res.tempFilePath)
					}
				});
			},
			uploadImageServicet(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.hdjs_video = res.url
					this.hdjs_video_text = image_item
				}).catch(err => {});
			},
			delvideo() {
				this.hdjs_video = ""
				this.hdjs_video_text = ""
			}
		}
	}
</script>

<style lang="scss" scoped>
	.huibtn {
		background: #F9F9F9;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		padding: 15rpx 30rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #777777;
	}

	.lvbtn {
		background: #DFFFED;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		padding: 15rpx 30rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #1BA95B;
	}

	.touxiang {
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;

		image {
			border-radius: 50%;
			width: 100%;
			height: 100%;
		}
	}

	.dizneo {
		height: 500rpx;
		overflow: auto;
	}

	.xiaolbtn {
		width: 112rpx;
		height: 48rpx;
		background: rgba(27, 169, 91, 0.1);
		border-radius: 24rpx 24rpx 24rpx 24rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #1BA95B;
		border: 2rpx solid #1BA95B;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	page {

		background: #F9F9F9;
	}

	.shanchu {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
	}

	.shanchu2 {
		position: absolute;
		bottom: -10rpx;
		right: -50%;
		transform: translate(0, -50%);
	}

	.shilitu {
		width: 160rpx;
		height: 160rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.shangc {
		width: 160rpx;
		height: 160rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>